package com.situ.mall.controller.front;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.json.JsonReadContext;
import com.situ.mall.entity.Cart;
import com.situ.mall.entity.User;
import com.situ.mall.service.ICartService;
import com.situ.mall.util.JSONResult;
import com.situ.mall.vo.CartVO;

@Controller
@RequestMapping("/cart")
public class FrontCartController {
	@Autowired
	private ICartService cartService;

	@RequestMapping("/insert.shtml")
	@ResponseBody
	public JSONResult insert(Cart cart, HttpSession session) {
		User user = (User) session.getAttribute("user");
		cart.setUserId(user.getId());
		cart.setChecked(1);
		cartService.insert(cart);
		return JSONResult.ok("插入成功");
	}
	
	@RequestMapping("/getCartListPage.shtml")
	public String getCartListPage(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		List<CartVO> list = cartService.selectByUserId(user.getId());
		model.addAttribute("list", list);
		return "/jsp/front/cart_list.jsp";
	}
	
	@RequestMapping("/deleteById")
	@ResponseBody
	public JSONResult deleteById(Integer id) {
		cartService.deleteById(id);
		return JSONResult.ok("删除成功");
	}
	
	@RequestMapping("/updateCheckedStatus")
	@ResponseBody
	public JSONResult updateCheckedStatus(Integer id, Integer checked) {
		cartService.updateCheckedStatus(id, checked);
		return JSONResult.ok("更新成功");
	}
}
